﻿using Coffee_Restaurant.Database;
using System;
using System.Linq;

namespace Coffee_Restaurant.DAL
{
    class DinnerMenuDa
    {
        public int Delete(int id)
        {
            try
            {
                using (var db = new DatabaseEntities())
                {
                    var model = db.DinnerMenus.FirstOrDefault(t => t.DinnerMenuId == id);
                    if (model != null)
                    {
                        db.DinnerMenus.Remove(model);
                        return db.SaveChanges();
                    }
                }
            }
            catch (Exception)
            {

                return 0;
            }
            return 0;
        }

        public int GetId(string code)
        {
            using (var db = new DatabaseEntities())
            {
                var model = db.DinnerMenus.FirstOrDefault(t => t.Code == code);
                if (model != null)
                {
                    return model.DinnerMenuId;
                }
            }
            return 0;
        }

        public static int InsertUpdate(DinnerMenu dinnerMenu)
        {
            try
            {
                using (var db = new DatabaseEntities())
                {
                    if (dinnerMenu.DinnerMenuId > 0)
                    {
                        var model = db.DinnerMenus.FirstOrDefault(x => x.DinnerMenuId == dinnerMenu.DinnerMenuId);
                        db.Entry(model).CurrentValues.SetValues(dinnerMenu);
                    }
                    else
                    {
                        db.DinnerMenus.Add(dinnerMenu);
                    }
                    return db.SaveChanges();
                }
            }
            catch (Exception)
            {

                return 0;
            }
        }
        public static int Update(DinnerMenu dinnerMenu)
        {
            try
            {
                using (var db = new DatabaseEntities())
                {
                    if (dinnerMenu.DinnerMenuId > 0)
                    {
                        var model = db.DinnerMenus.FirstOrDefault(x => x.DinnerMenuId == dinnerMenu.DinnerMenuId);
                        db.Entry(model).CurrentValues.SetValues(dinnerMenu);
                    }
                    else
                    {
                        db.DinnerMenus.Add(dinnerMenu);
                    }
                    return db.SaveChanges();
                }
            }
            catch (Exception)
            {

                return 0;
            }
        }
    }
}
